package com.hpin.mlxteacher.ui;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.fragment.BabyTreeFragment;
import com.hpin.mlxteacher.fragment.ChildFragment;
import com.hpin.mlxteacher.fragment.MyFragement;
import com.hpin.mlxteacher.fragment.NewsFragment;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.widget.ActionSheetDialog;

/**
 * Created by Administrator on 2015/7/14.
 */
public class HomeActivity extends BaseActivity {

    private FrameLayout fl_base;
    /**
     * fragment 管理者
     */
    private FragmentManager manager;
    private NewsFragment newsFragment;
    private ChildFragment childFragment;
    private BabyTreeFragment babyTreeFragment;
    private MyFragement myFragement;
    private TextView tv_message;
    private TextView tv_child;
    private TextView tv_baby_tree;
    private TextView tv_my;
    private ImageView iv_message;
    private ImageView iv_child;
    private ImageView iv_photo;
    private ImageView iv_baby_tree;
    private ImageView iv_my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.activity_home);

        initWidget();

        initFragment();

    }

    private void initWidget() {

        fl_base = (FrameLayout) findViewById(R.id.fl_base);

        RelativeLayout rl_message = (RelativeLayout) findViewById(R.id.rl_message);
        RelativeLayout rl_child = (RelativeLayout) findViewById(R.id.rl_child);
        RelativeLayout rl_baby_tree = (RelativeLayout) findViewById(R.id.rl_baby_tree);
        RelativeLayout rl_my = (RelativeLayout) findViewById(R.id.rl_my);

        iv_message = (ImageView) findViewById(R.id.iv_message);
        iv_child = (ImageView) findViewById(R.id.iv_child);
        iv_photo = (ImageView) findViewById(R.id.iv_photo);
        iv_baby_tree = (ImageView) findViewById(R.id.iv_baby_tree);
        iv_my = (ImageView) findViewById(R.id.iv_my);

        tv_message = (TextView) findViewById(R.id.tv_message);
        tv_child = (TextView) findViewById(R.id.tv_child);
        tv_baby_tree = (TextView) findViewById(R.id.tv_baby_tree);
        tv_my = (TextView) findViewById(R.id.tv_my);

        rl_message.setOnClickListener(this);
        rl_child.setOnClickListener(this);
        rl_baby_tree.setOnClickListener(this);
        rl_my.setOnClickListener(this);
        iv_photo.setOnClickListener(this);
    }

    private void initFragment() {

        manager = getFragmentManager();

        chooseFragment(0);
    }

    @Override
    public void widgetClick(View v) {

        setSelectFalse();

        switch (v.getId()) {

            case R.id.rl_message:

                //消息

                chooseFragment(0);

                //TODO 改变字体颜色和图片
                iv_message.setSelected(true);
                tv_message.setTextColor(getResources().getColor(R.color.red));
                break;
            case R.id.rl_child:

                //孩子
                chooseFragment(1);

                iv_child.setSelected(true);
                tv_child.setTextColor(getResources().getColor(R.color.red));
                break;
            case R.id.rl_baby_tree:

//宝宝树
                chooseFragment(3);

                iv_baby_tree.setSelected(true);
                tv_baby_tree.setTextColor(getResources().getColor(R.color.red));
                break;
            case R.id.rl_my:
                //我的
                chooseFragment(4);

                iv_my.setSelected(true);
                tv_my.setTextColor(getResources().getColor(R.color.red));

                break;
            case R.id.iv_photo:

                showChoose();

                break;

        }
    }


//    @Override
//    public void onCheckedChanged(RadioGroup group, int checkedId) {
//
//        setSelectFalse();
//
//        switch (checkedId) {
//
//            case R.id.rb_newsfragment:
//
//
//                break;
//            case R.id.rb_childfragment:
//
//                break;
//            case R.id.rb_photofragment:
//                //拍照
//                //添加照片 弹出选择对话框
////                chooseFragment(2);
//
////                rb_photofragment.setSelected(true);
////                rb_photofragment.setTextColor(getResources().getColor(R.color.red));
//                break;
//            case R.id.rb_babytreefragment:
//
//
//                break;
//            case R.id.rb_myfragment:
//
//
//                break;
//
//        }
//
//    }

    /**
     * 选择对应的fragment
     *
     * @param index 对应的fragment的角标
     */
    private void chooseFragment(int index) {

        FragmentTransaction transaction = null;
        if (manager != null) {
            transaction = manager.beginTransaction();
        }
        // 先移除掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
//        removeFragments(transaction);

        if (transaction == null)
            return;

        switch (index) {

            case 0:
                //消息
                if (newsFragment == null)
                    newsFragment = new NewsFragment();

                transaction.replace(R.id.fl_base, newsFragment, Constant.NEWS);

                break;

            case 1:

                if (childFragment == null)
                    childFragment = new ChildFragment();

                transaction.replace(R.id.fl_base, childFragment, Constant.CHILD);

                break;

            case 2:

                break;
            case 3:
                if (babyTreeFragment == null)
                    babyTreeFragment = new BabyTreeFragment();

                transaction.replace(R.id.fl_base, babyTreeFragment, Constant.BABYTREE);

                break;
            case 4:

                if (myFragement == null)
                    myFragement = new MyFragement();

                transaction.replace(R.id.fl_base, myFragement, Constant.MY);

                break;

        }

        if (transaction != null) {
//            transaction.commit();
            transaction.commitAllowingStateLoss();
        }

    }

    /**
     * 移除fragment
     *
     * @param transaction
     */
    private void removeFragments(FragmentTransaction transaction) {
        if (transaction == null)
            return;

        if (newsFragment != null) {
            transaction.remove(newsFragment);
        }
        if (childFragment != null) {
            transaction.remove(childFragment);
        }
        if (babyTreeFragment != null) {
            transaction.remove(babyTreeFragment);
        }
        if (myFragement != null) {
            transaction.remove(myFragement);
        }

    }


    /**
     * 弹出选择照片的对话框
     */
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 拍照
//                                getTakePhoto();

                            }

                        })
                .addSheetItem("去相册选择", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 从相册中选择
//                                getFromPhotoAlbum();
                            }
                        }).show();

    }


    /**
     * 把所有的图标都变灰
     * 字体还原
     */
    private void setSelectFalse() {

        iv_message.setSelected(false);
        iv_child.setSelected(false);
        iv_photo.setSelected(false);
        iv_baby_tree.setSelected(false);
        iv_my.setSelected(false);

        tv_message.setTextColor(getResources().getColor(R.color.text_color));
        tv_child.setTextColor(getResources().getColor(R.color.text_color));
        tv_baby_tree.setTextColor(getResources().getColor(R.color.text_color));
        tv_my.setTextColor(getResources().getColor(R.color.text_color));

    }

    private long mPressedTime = 0;

    @Override
    public void onBackPressed() {

        long mNowTime = System.currentTimeMillis();//获取第一次按键时间

        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差

            showToast("再按一次退出程序");

            mPressedTime = mNowTime;

        } else {//退出程序
            finish();
            System.exit(0);
        }
    }
}
